import { Engine } from "../../Engine";
import { any } from "../../core/logic";
import { Object3D } from "../../entity/Object3D";
import { Component, ComponentType } from "../Component";
class Plot extends Component {
    static that = null;
    constructor(opts = {}) {
        opts.type = ComponentType.Plot;
        super(opts);
        this.enable = false;
        this.mode = any(opts.mode, "mark");//两种模式：mark(标)|draw(绘);
        this._cursor = any(opts.cursor, "");
        this.drawConfig = {};
        Plot.that = this;
    }
    set cursor(v) {
        this._cursor = v;
        return this;
    }
    get cursor() {
        return this._cursor;
    }
    get pick() {
        return Engine.instance.register.manager(ComponentType.Pick).get("x");
    }
    setPlotContent(opts = {}) {
        this.drawConfig.entity = opts.entity;
        this.drawConfig.group = opts.group;
        this.drawConfig.instance = any(opts.instance, true);
        this.drawConfig.scale = any(opts.scale, [1, 1, 1]);
        this.drawConfig.rotation = any(opts.rotation, [0, 0, 0]);
        this.drawConfig.randomScale = any(opts.randomScale, 0);
        this.drawConfig.randomRotation = any(opts.randomRotation, 0);
    }
    active() {
        if (this.enable)
            return;
        this.enable = true;
        // this.on("dblclick",this.deactive);
        if (this.mode === "mark") {
            if (this.cursor) {
                Engine.instance.canvas.style.cursor = `url(${this.cursor}),auto`;
            }
            this.pick.select(this.mark);
        } else {
            //暂时不做
        }
        if (Plot.that.callback) {
            Plot.that.callback("active");
        }

    }
    deactive() {
        if (!this.enable)
            return;
        this.enable = false;
        // this.off("dblclick",this.deactive);
        if (this.mode === "mark") {
            Engine.instance.canvas.style.cursor = "default";
            if (this.drawConfig && this.drawConfig.entity) {
                this.drawConfig.entity = null;
            }

            this.pick.offSelect(this.mark);
        } else {
            //暂时不做
        }
        if (Plot.that.callback) {
            Plot.that.callback("deactive");
        }
    }
    draw() {

    }
    mark(result) {
        if(result.entity==undefined){
            if (Plot.that.markCallback) {
                Plot.that.markCallback(false);
            }
            return false;
        }
        const { entity, group, scale, randomScale, rotation, randomRotation, instance } = Plot.that.drawConfig;
        if (result.event.originEvent.shiftKey) {
            let et = result.entity;
            let instanceIndex = result.instanceIndex;
            if (instanceIndex != undefined) {
                let root = et.getRoot();
                let c = root.getAllInstance();
                for (let i = 0; i < c.length; i++) {
                    c[i].trans.removeByIndex(instanceIndex);
                }
                Plot.that.markCallback({ type: 'delete', instanceIndex, entity: root });
                //et.trans.removeByIndex(instanceIndex); 
            }else{
                if(result.entity && result.entity.getRoot().isPlot){
                    result.entity.destroy();
                }
            }
            return;
        }
        if (!entity) {
            return;
        }
        let position = result.worldposition;
        if (result.entity) {
            if (result.entity.getRoot().isPlot) {
                let z = 0;
                let ti = result.entity.getAllInstance()[0]
                if (ti && result.instanceIndex) {
                    if (ti.trans) {
                        if (ti.trans._transArr[result.instanceIndex]) {
                            z = ti.trans._transArr[result.instanceIndex].position.z;
                        }
                    }
                    ti.trans._transArr[0].position.z
                }
                position = [result.worldposition[0], result.worldposition[1], 0];
            }
        }
        let [sx, sy, sz] = scale;
        sx += Math.random() * randomScale;
        sy += Math.random() * randomScale;
        sz += Math.random() * randomScale;
        let [rx, ry, rz] = rotation;
        rx += Math.random() * randomRotation;
        ry += Math.random() * randomRotation;
        rz += Math.random() * randomRotation;
        if (instance) {
            entity.getRoot().isPlot = true;
            if (entity.group == "default") {//未添加到场景
                group.addEntity(entity);
                entity.addInstance({ position, scale: [sx, sy, sz], rotation: [rx, ry, rz] }, true);
            } else {
                entity.addInstance({ position, scale: [sx, sy, sz], rotation: [rx, ry, rz] });
            }
            if (Plot.that.markCallback) {
                Plot.that.markCallback({ type: 'add', position, scale: [sx, sy, sz], rotation: [rx, ry, rz], entity });
            }
        } else {//非实例化绘制
            let material = new w.material.BasicMaterial({
                color: entity.color, alphaCutoff: 0.0,
                blending: "NormalBlending",
                defuseLight: true, map: entity.map
            });
            let vex = new Float32Array([
                0, 0, 0,
            ])
            let geometry = new w.geometry.PointGeometry({ position: vex, size: entity.size, shape: 'rect', verticalLocation: "bottom" })
            let mesh = new w.component.Mesh({ geometry, material });
            let object = new w.Object3D({});
            object.isPlot=true;
            object.addComponent(mesh);
            object.trans.position = position;
            object.trans.scale = [sx, sy, sz];
            object.trans.rotation = [rx, ry, rz];
            group.addEntity(object);
            if (Plot.that.markCallback) {
                Plot.that.markCallback({ position, scale: [sx, sy, sz], rotation: [rx, ry, rz], entity:object });
            }
        }
        return true;
    }
}
export { Plot }